/*
 * ﻿Copyright (C) 2011-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.aspectjutil.eventmanager;

import java.lang.reflect.Method;

import junit.framework.Assert;

import org.junit.Test;
import org.slf4j.LoggerFactory;

public class EventHandlerTestWithEventRecipientChild extends EventHandlerTestTemplate {
	static {
		logger = LoggerFactory.getLogger( EventHandlerTestWithEventRecipientChild.class);
	}
	
	// Tests for @EventListener method inheritance ------------------------------------------------
	@Test
	public void testByTriggeringCalculationEvent2() throws Throwable {
		// Instantiate EventRecipientChild object -------------------------------------------------
		Object eventRecipientChildObj 
		= EventManagerTestUtils.eventRecipientChildFactory( childDelegationClassLoader);
		// ----------------------------------------------------------------------------------------

		// Execute EventProducer.calculate method -------------------------------------------------
		Object eventProducerObj = EventManagerTestUtils.eventProducerFactory( childDelegationClassLoader);
		Method calculateMethod 
		= EventManagerTestUtils.getEventProducerCalculateMethod( eventProducerObj.getClass());
		Integer calculationResult = (Integer)calculateMethod.invoke( eventProducerObj, 4, 8);
		// ----------------------------------------------------------------------------------------
		
		// Verifications --------------------------------------------------------------------------
		Assert.assertEquals( 
				EventManagerTestUtils.getEventTraceListDump( eventTraceList) + "\n", 
				3, 
				eventTraceList.size()
				);
		
			int eventTraceIndex = -1;
			Assert.assertTrue( 
					EventManagerTestUtils.getEventTraceListDump( eventTraceList),					
					eventTraceList.get( ++eventTraceIndex)
					.contains( 
							EventManagerTestUtils.EventRecipientClassName
							+ ".staticListenerOfCalculationCompletion"
							) 
					);
			Assert.assertTrue( 
					EventManagerTestUtils.getEventTraceListDump( eventTraceList),					
					eventTraceList.get( ++eventTraceIndex)
					.contains( 
							EventManagerTestUtils.EventRecipientClassName
							+ ".onCalculationCompletion"
							) 
					);
			Assert.assertTrue( 
					EventManagerTestUtils.getEventTraceListDump( eventTraceList),					
					eventTraceList.get( ++eventTraceIndex)
					.contains( 
							eventRecipientChildObj.getClass().getName() 
							+ ".staticListenerOfCalculationCompletion"
							) 
					);
			
			for( eventTraceIndex = 0; eventTraceIndex < eventTraceList.size(); eventTraceIndex++) {
				Assert.assertEquals( 
						EventManagerTestUtils.getCalculationEventClass( childDelegationClassLoader),
						triggeredEventTraceList.get( eventTraceIndex)
						);
				Class<?> eventerInfoClass 
				= EventManagerTestUtils.getEventerInfoClass( childDelegationClassLoader);
				Object eventerInfo = eventerInfoClass.cast( eventerTraceList.get( eventTraceIndex));
					Assert.assertEquals( 
							EventManagerTestUtils.getEventerObjOfEventerInfo( eventerInfo),
							eventProducerObj
							);
					Assert.assertEquals( 
							EventManagerTestUtils.getEventerMethodOfEventerInfo( eventerInfo).getName(),
							"calculate"
							);
				Assert.assertEquals( 
						(Integer)eventResultTraceList.get( eventTraceIndex), 
						calculationResult
						);
			} // for
		// ----------------------------------------------------------------------------------------
	}
	// --------------------------------------------------------------------------------------------
}
